<?php
/**
 * Membership account upgrade helper script
 * 
 * Detect member biller and redirect to appropriate Nats membership upgrade URL
 * For now supports:
 * - Epoch
 * - Netbilling
 *  
 * @name		membership upgrade
 * @version		1.0
 * @uses		Nats4space_Users_Management
 * @package		nats-space
 * @see			http://code.google.com/p/nats-space/
 * @copyright	2009
 * @author		Joseph C. <joseph.cheys@gmail.com>
 */

/**
 * SCRIPT CONFIGURATION
 */

/**
 * Affects errors output and debug information output, redirect will be disabled
 */ 
define('DEBUG_MODE', false);

/**
 * Site ID in Nats database (this can be found is "Sites Admin")
 */
define('SITE_ID', '2');

/**
 * NATS upgrade interface domain
 * 
 * generally the URL to your NATS
 * 
 * but you can create some cool sub-domain for ex. "secure.yourdomain.com"
 * to process your membership upgrade transactions "securely" :)
 */
define('LINK_DOMAIN', 'secure.exoticpays.com');


/**
 * Full membership area URL
 * 
 * will be used to redirect member to full membership area
 * in case we need that
 */
define('MEMBERSHIP_AREA_URL', 'members.bigbuttshakers.com');



/**
 * Library path
 * set to /framework...path/library/
 *  
 * (can be blank if this path is added into your inlude path)
 * NOTE! don't forget to add trailing slash
 */
define('LIB_DIR', '/web/sites/pookey/library/');



/**
 * Epoch specific configuration
 */

/**
 * Paycom's ti_code for this site
 */
define('EPOCH_TI_CODE', 'bigbuttshakers');















/**
 * NATS config
 * You can define the path to NATS config file
 * and we will read the database accesses from that file  
 * OR... (to be continued, at the bottom  V )
 */
define('NATS_CONFIG', '/web/sites/pookey/nats4/includes/config.php');

if (file_exists(NATS_CONFIG)) { // read database accesses
	
	require_once NATS_CONFIG; 
	define('DB_SERVER', $config['DB_SERVER']);
	define('DB_USER', $config['DB_USER']);
	define('DB_PASSWORD', $config['DB_PASSWORD']);
	define('DB_DB', $config['DB_DB']);

} else {
/**
 * ...OR you can provide the database accesses directly here
 */
	define('DB_SERVER',		'localhost');
	define('DB_USER',		'--databse user name --');
	define('DB_PASSWORD',	'--database password --');
	define('DB_DB',			'--database name--');
}





/**
 * END OF CONFIGURATION
 * 
 * WARNING!!! Do not modify the code above 
 * unleast you are realy know what you are doing.
 */

/**
 * Read required session/environment data
 */

 
/**
 * Asuming that this member was authenticated via mod_auth_mysql or similar
 */
define('MEMBER_LOGIN', $_SERVER['PHP_AUTH_USER']); 

/**
 * Init includepath
 */
$paths = explode(PATH_SEPARATOR, get_include_path());
$paths[] = LIB_DIR;
set_include_path(implode(PATH_SEPARATOR, $paths));
unset($paths);

require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('Nats4space_');

/**
 * Init database interface
 */
$config = array(
	  'host'        => DB_SERVER
    , 'username'    => DB_USER
    , 'password'    => DB_PASSWORD
    , 'dbname'      => DB_DB
);

$db = Zend_Db::factory('PDO_MYSQL', $config);

/**
 * Biller detection
 */
$UserManager = new Nats4space_Users_Management($db, SITE_ID);

/**
 * Check if membership level is not trial
 * there is no any need to process billing for this customer if it already has full membership
 * so in that case lets asume that he entered this script exidently 
 * and redirect customer to membership area
 */
if (!$UserManager->isTrial(MEMBER_LOGIN)) {
    header('Location: ' . MEMBERSHIP_AREA_URL);
    end();
}

$redirectUrl = $UserManager->getUpgradeUrl(MEMBER_LOGIN, LINK_DOMAIN, SITE_ID, EPOCH_TI_CODE);

/**
 * Redirect member
 */
if (!DEBUG_MODE) {
	header('Location: ' . $redirectUrl);
} else {
	echo 'Member wiill be redirected here: ' . $redirectUrl;
	die();
}